﻿using System;
using System.Web.UI;
using AgileDashboard.AppCode;
using AgileDashboard.controls;

namespace AgileDashboard
{
    public partial class _Default : BasePage
    {
        public int ReleaseGraphWidth = 637; //default
        public int TeamGraphHeight = 275; //default
        public Iteration CurrentIteration;

        protected void Page_Load(object sender, EventArgs e)
        {

            var iterationSchedule = Properties.Settings.Default.DefaultIterationSchedule;
            
            var teams = Properties.Settings.Default.TeamParams;

            foreach (var team in teams)
            {
                if (string.IsNullOrEmpty(team))
                {
                    continue;
                }

                var teamIterationSchedule = iterationSchedule;
                var teamParams = team.Split('|');
                var teamName = teamParams[0];

                // you can optionally specify an alternate iteration schedule. If it isn't used, the default iteration schedule will be used
                if (teamParams.Length > 1)
                {
                    teamIterationSchedule = teamParams[1];
                }

                // pattern for loading cached user controls dynamically borrowed from: http://msdn.microsoft.com/en-us/magazine/cc163577.aspx

                // Load the user control
                var control = LoadControl("~/controls/DynamicControlLoader.ascx");
                Teams.Controls.Add(control);

                // Set the control properties (if possible)
                var uc = control as DynamicControlLoader;
                if (uc != null)
                {
                    uc.Height = 398;
                    uc.Width = 148;
                    uc.CssClass = "horizontalDynamicPlaceholder";
                    uc.UserControlPath = Server.UrlEncode("~/controls/TeamIterationGraph.ascx");
                    uc.Params = string.Format("IterationSchedule={0}&GraphHeight={1}&Team={2}", this.Server.UrlEncode(teamIterationSchedule), this.TeamGraphHeight, this.Server.UrlEncode(teamName));
                }
            }
            
            var dao = new IterationDao(new RestDataConnection());
            this.CurrentIteration = dao.GetCurrentIterationBySchedule(iterationSchedule, false);

            var projectDao = new ProjectDao(new RestDataConnection());

            var projects = projectDao.GetAgileDashboardConfiguredProjects(false);
            foreach (var project in projects)
            {
                // pattern for loading cached user controls dynamically borrowed from: http://msdn.microsoft.com/en-us/magazine/cc163577.aspx

                // Load the user control
                var control = LoadControl("~/controls/DynamicControlLoader.ascx");
                Releases.Controls.Add(control);

                // Set the control properties (if possible)
                var uc = control as DynamicControlLoader;
                if (uc != null)
                {
                    uc.Height = 90;
                    uc.Width = 1086;
                    uc.UserControlPath = Server.UrlEncode("~/controls/ReleaseGraph.ascx");
                    uc.Params = string.Format("IterationSchedule={0}&GraphWidth={1}&Release={2}", this.Server.UrlEncode(iterationSchedule), this.ReleaseGraphWidth, this.Server.UrlEncode(project.Name));
                }
            }

            var builds = Properties.Settings.Default.BuildParams;
            foreach (var build in builds)
            {
                if (string.IsNullOrEmpty(build))
                {
                    continue;
                }

                var buildParams = build.Split('|');
                var buildName = buildParams[0];
                var buildServerUrl = buildParams[1];

                // pattern for loading cached user controls dynamically borrowed from: http://msdn.microsoft.com/en-us/magazine/cc163577.aspx

                // Load the user control
                var control = LoadControl("~/controls/DynamicControlLoader.ascx");
                Builds.Controls.Add(control);

                // Set the control properties (if possible)
                var uc = control as DynamicControlLoader;
                if (uc != null)
                {
                    uc.Height = 225;
                    uc.Width = 266;
                    uc.CssClass = "horizontalDynamicPlaceholder";
                    uc.UserControlPath = Server.UrlEncode("~/controls/BuildBox.ascx");
                    uc.Params = string.Format("Title={0}&BuildFeedUrl={1}", this.Server.UrlEncode(buildName), this.Server.UrlEncode(buildServerUrl));
                }
            }
        }
    }
}